<template>
  <el-dialog
    :visible.sync="interalVisible"
    :title="title"
    :width="width"
    :size="size"
    top="10vh"
    modal
    class="dialog-with-serach-selection"
    modal-append-to-body
    append-to-body
    lock-scroll
    show-close
    :close-on-click-modal="false"
    @close="closeDialog"
  >
    <div class="search-container">
      <el-input
        v-model="pagerequest.filter"
        placeholder="搜索..."
        style="width:700px"
        :size="size"
      />
      <el-button
        type="primary"
        icon="el-icon-search"
        :size="size"
        :loading="loading"
        @click="getpage"
      />
    </div>

    <div class="table-container">
      <el-table
        ref="multipleTable"
        v-loading="loading"
        :size="size"
        :height="tableHeight"
        :data="list"
        border
        stripe
        style="width: 100%"
        @selection-change="handleSelectionChange"
        @row-click="handleRowClick"
      >
        <el-table-column
          type="selection"
          width="55"
        />

        <slot />

      </el-table>
    </div>

    <div class="pagination-container">
      <el-pagination
        :size="size"
        :page-sizes="[10, 50, 100, 150]"
        :page-size="pageSize"
        :total="totalCount"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pagesizeChange"
        @current-change="pageChange"
      />
    </div>
    <div slot="footer">
      <el-button :size="size" @click="closeDialog">{{ L('Cancel') }}</el-button>
      <el-button
        type="primary"
        :size="size"
        :loading="dialogLoading"
        @click="saveDialog"
      >
        <svg-icon icon-class="save" /> {{ L('OK') }}</el-button>
    </div>
  </el-dialog>
  <!-- 调用 this.$refs.CreateOrEditOu.open() -->
</template>

<script>
import searchDialogPagedRequest from '@/store/entities/SearchDialogPagedRequest'

export default {
  name: 'DialogWithSearchSelection',
  props: {
    size: {
      type: String,
      default: 'small'
    },
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    width: {
      type: String,
      default: '800px'
    },
    entityName: {
      type: String,
      default: ''
    },
    searchSelectionMethod: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      interalVisible: false,
      dialogLoading: false,
      keyword: '',
      pagerequest: { ...searchDialogPagedRequest },
      requestParam: {},
      tableData: [],

      multipleSelection: []
    }
  },
  computed: {
    tableHeight() {
      return (document.body.clientHeight * 0.45) + 'px'
    },
    list() {
      return this.$store.state[this.entityName].searchList
    },
    totalCount() {
      return this.$store.state[this.entityName].searchTotalCount
    },
    currentPage() {
      return this.$store.state[this.entityName].searchCurrentPage
    },
    pageSize() {
      return this.$store.state[this.entityName].searchPageSize
    },
    loading() {
      return this.$store.state[this.entityName].searchLoading
    }
  },
  watch: {
    visible(val) {
      this.interalVisible = val
    }
  },
  methods: {
    initialize(requestParam) {
      this.$refs.multipleTable.clearSelection()
      this.$store.commit(`${this.entityName}/SET_INIT`, this.entityName)
      this.pagerequest.filter = ''
      this.requestParam = requestParam
      this.getpage()
    },
    pagesizeChange(pagesize) {
      this.$store.commit(`${this.entityName}/SET_PAGE_SIZE`, pagesize)
      this.getpage()
    },
    pageChange(page) {
      this.$store.commit(`${this.entityName}/SET_CURRENT_PAGE`, page)
      this.getpage()
    },
    async getpage() {
      this.pagerequest.maxResultCount = this.pageSize
      this.pagerequest.skipCount = (this.currentPage - 1) * this.pageSize

      // filters
      for (var i in this.requestParam) {
        this.pagerequest[i] = this.requestParam[i]
      }

      await this.$store.dispatch({
        type: `${this.entityName}/getSearchSelectionAll`,
        data: this.pagerequest
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleRowClick(row) {
      this.$refs.multipleTable.toggleRowSelection(row)
    },

    closeDialog() {
      this.$emit('update:visible', false)
    },
    saveDialog() {
      this.dialogLoading = true
      this.$emit('save', async saveHandle => {
        await saveHandle(this.multipleSelection.map(m => m.value)).finally(() => {
          this.dialogLoading = false
        })
        // this.$message({
        //   type: 'success',
        //   message: this.L('SavedSuccessfully')
        // })
        this.$emit('update:visible', false)
      })
    }
  }
}
</script>

<style lang="scss">
.dialog-with-serach-selection {
  .table-container {
    margin-top: 20px;
  }
}
</style>

